/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasi?                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "userdialog.h"
#include "globals.h"
#include "trackerdb.h"
#include "exceptions.h"
#include "exceptionhandler.h"

#include <QtGui>

UserDialog::UserDialog(QWidget* parent, Qt::WFlags fl)
		: QDialog(parent, fl)
{
	setupUi(this);

	connect(m_buttons, SIGNAL(accepted()), this, SLOT(doRegister()));
}

UserDialog::~UserDialog()
{}

void UserDialog::doRegister()
{
	if (m_username->text().isEmpty() || m_password->text().isEmpty() || m_email->text().isEmpty())
	{
		QMessageBox::information(this, tr("QTracker"), tr("You must fill in all fields."));
		return;
	}

	TrackerDB* db = Globals::instance().db();

	try
	{
		bool userExists = FALSE;
		bool emailExists = FALSE;

		if (db->userExists(m_username->text(), m_email->text(), &userExists, &emailExists))
		{
			QString msg;

			if (userExists)
				msg = tr("The user with username \"") + m_username->text() + tr("\" already exists.\nPlease choose another username.");
			else
				msg = tr("The user with email \"") + m_email->text() + tr("\" already exists.\nPlease choose another email.");

			QMessageBox::warning(this, tr("QTracker"), msg, QMessageBox::Ok);
			return;
		}

		db->registerUser(m_username->text(), m_password->text(), m_email->text(), m_name->text());
	}
	catch (DBException* ex)
	{
		ExceptionHandler::instance().protocolException(this->thread(), ex, TRUE);
		QMessageBox::critical(this, tr("QTracker"), tr("A database error occurred. Check out your log file for more information."));

		return;
	}

	QMessageBox::information(this, tr("QTracker"), tr("User registered."));
	accept();
}

